/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

// 生成 secret
@Component
@PropertySource({ "classpath:workflow.properties" })
public class SecretUtil {
   private final static Logger logger= LoggerFactory.getLogger(SecretUtil.class);
    // 流程公共秘钥
    @Value("${workflow.RSApublickey}")
    private String RSApublickey;

    private static String KEY;

    /**
     * 加密
     * @param appCode
     * @return
     */
    public static String encrypt(String appCode) {
        try {
            Objects.requireNonNull(appCode, "要加密的内容不能为空");
            appCode += "-" + System.currentTimeMillis();
//            KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKihfalV5Kt1wwKBDalLK6hGZAc3kxz33B36oeOOZMXTVTtPEHBK7eIXMvOwLMbB7IWkU/9ppZuVG8+tJs0BeekCAwEAAQ==";
            return RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(KEY), appCode.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException("加密失败");
        }
    }

    @PostConstruct
    public void init() {
        KEY = this.RSApublickey;
    }
    
    public static void main(String[] args) {
    	String s = encrypt("safe_zh");
        logger.info(s);
    }
}
